package com.lu.manage.modular.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.manage.core.common.constant.SystemConstant;
import com.lu.manage.core.tips.SuccessTip;
import com.lu.manage.core.utils.ToolUtil;
import com.lu.manage.modular.system.mapper.DatasourceMapper;
import com.lu.manage.modular.system.model.Datasource;
import com.lu.manage.modular.system.service.DatasourceService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Project sKnowledge_Blog
 * @Author: zhanglu
 * @Date: 2019-07-14 16:06:25
 * @Description: 数据源 服务实现类
 */
@Service
public class DatasourceServiceImpl extends ServiceImpl<DatasourceMapper, Datasource> implements DatasourceService {

    @Override
    public Object listPage(Map<String, Object> map) {
        SuccessTip successTip = new SuccessTip();
        try {
            Page page = new Page(Integer.valueOf((String)map.get(SystemConstant.PAGE)), Integer.valueOf((String)map.get(SystemConstant.LIMIT)));
            QueryWrapper<Datasource> queryWrapper = new QueryWrapper<>();
            map.forEach((k, v) -> {
                if(!k.equals(SystemConstant.PAGE) && !k.equals(SystemConstant.LIMIT)){
                    if(k.equals(SystemConstant.SEARCH_WORDS)){
                        queryWrapper.like("name", v);
                    }else {
                        queryWrapper.eq(k, v);
                    }
                }
            });
            IPage<Datasource> datasourceList = this.baseMapper.selectPage(page, queryWrapper);
            successTip.setCount(datasourceList.getTotal());
            successTip.setData(datasourceList.getRecords());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return successTip;
    }

    @Override
    public Object list(Map<String, Object> map) {
        List<Datasource> datasourceList = new ArrayList<>();
        try {
            QueryWrapper<Datasource> queryWrapper = new QueryWrapper<>();
            if(ToolUtil.isNotEmpty(map)){
               map.forEach((k, v) -> {
                   queryWrapper.eq(k, v);
               });
            }
            datasourceList = this.baseMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return datasourceList;
    }

    @Override
    public Object add(Datasource datasource) {
        return this.baseMapper.insert(datasource);
    }

    @Override
    public Object update(Datasource datasource) {
        return this.baseMapper.updateById(datasource);
    }

    @Override
    public Object delete(String data) {
        List<Datasource> datasourceList = JSONArray.parseArray(data, Datasource.class);
        List<String> ids = datasourceList.stream().map(Datasource::getId).collect(Collectors.toList());
        if(ToolUtil.isNotEmpty(ids)){
            this.baseMapper.deleteBatchIds(ids);
        }
        return new SuccessTip();
    }

}
